package com.after.saas.mall.admin.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: hjchen
 * @Contact: afterchj@foxmail.com
 * @Date: 2020/12/21 10:12
 */

public class CodeGenerator {

    public static void main(String[] args) {
        //代码生成器
        AutoGenerator mpg = new AutoGenerator();
        //全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = "E:\\IdeaProjects\\work\\dev\\saas-cloud\\saas-mall\\mall-admin";
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setServiceName("%sService"); //设置生成的service接口的名字的首字母是否为I
        gc.setAuthor("hjchen");
        gc.setFileOverride(false);     //是否覆蓋已有文件 默认值：false
        gc.setOpen(false);           //是否打开输出目录 默认值:true
        gc.setEnableCache(false);    // XML 二级缓存
        gc.setBaseResultMap(true);   // XML ResultMap
        gc.setBaseColumnList(true);  // XML columList
        gc.setSwagger2(true);  // swagger2注释
        gc.setIdType(IdType.AUTO);
        mpg.setGlobalConfig(gc);

        //数据源配置
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setDbType(DbType.MYSQL)
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUrl("jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT")
                .setUsername("root")
                .setPassword("root");
        mpg.setDataSource(dsConfig);

        //包配置
        PackageConfig packageConfig = new PackageConfig();
        //设置生产代码的地方，一般是业务bus 模块
        packageConfig.setParent("com.after.saas.mall.admin")
                .setController("controller")
                .setService("service")
                .setMapper("mapper")
                .setEntity("model");
        mpg.setPackageInfo(packageConfig);

        //自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                //自定义输入文件名称
                return projectPath + "/src/main/resources/mapper/" + File.separator + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
//        //这里还可以设置模板的配置
        mpg.setTemplate(new TemplateConfig().setXml(null));
        //生成策略配置
        StrategyConfig strategy = new StrategyConfig();
        //类名生成策略：驼峰命名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        //字段名生成方式：驼峰命名
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //配置继承的父类
//        strategy.setSuperEntityClass("com.after.qims.backmall.model.BaseEntity");
//        strategy.setSuperControllerClass("com.example.demo.BaseController");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
//        strategy.setSuperEntityColumns("isDelete", "createTime", "updateTime");
        String[] tables = new String[]{"ums_admin", "ums_member"};
        //需要生成的表
        strategy.setInclude(tables);
        //strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
        //过滤表前缀
        strategy.setTablePrefix("ums_");

        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
